using System;
using System.Collections.Generic;
using System.Text;

namespace LuaNetInterface
{
    /// <summary>
    /// This class is a descriptor of a Lua function, containing information about the function.
    /// </summary>
    public class LuaFunctionDescriptor
    {
        #region Data Members
        /// <summary>
        /// The name of the function.
        /// </summary>
        private string _name;

        /// <summary>
        /// The documentation attached to the function.
        /// </summary>
        private string _documentation;

        /// <summary>
        /// The method parameters required by the function.
        /// </summary>
        private List<string> _parameters;

        /// <summary>
        /// The documentation for the method parameters required by the function.
        /// </summary>
        private List<string> _parameterDocumentation;

        /// <summary>
        /// The full documentation string created for the function.
        /// </summary>
        private string _documentationString;
        #endregion

        #region Properties
        /// <summary>
        /// Gets the name of the function.
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        /// <summary>
        /// Gets the documentation attached to the function.
        /// </summary>
        public string Documentation
        {
            get { return _documentation; }
        }

        /// <summary>
        /// Gets the method parameters required by the function.
        /// </summary>
        public List<string> Parameters
        {
            get { return _parameters; }
        }

        /// <summary>
        /// Gets the documentation for the method parameters required by the function.
        /// </summary>
        public List<string> ParameterDocumentation
        {
            get { return _parameterDocumentation; }
        }

        /// <summary>
        /// Gets the full documentation string created for the function.
        /// </summary>
        public string FullDocumentationString
        {
            get { return _documentationString; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Creates the Lua function descriptor.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="documentation"></param>
        /// <param name="parameters"></param>
        /// <param name="parameterDocumentation"></param>
        public LuaFunctionDescriptor(string name, string documentation,
            List<string> parameters, List<string> parameterDocumentation)
        {
            _name = name;
            _documentation = documentation;
            _parameters = parameters;
            _parameterDocumentation = parameterDocumentation;

            // Go through the different documentation strings and generate the full documentation string
            // for the function
            string header = name + "(%params%) - " + documentation;
            string body = "\n\n";
            string functionParams = "";

            bool first = true;
            
            for (int i = 0; i < parameters.Count; i++)
            {
                if (!first)
                    functionParams += ", ";

                functionParams += parameters[i];
                body += "\t" + parameters[i] + "\t\t" + parameterDocumentation[i] + "\n";

                first = false;
            }

            body = body.Substring(0, body.Length - 1);

            if (first)
                body = body.Substring(0, body.Length - 1);

            _documentationString = header.Replace("%params%", functionParams) + body;
        }

        /// <summary>
        /// Gets the documentation header of the Lua function.  This is the leading fragment of
        /// the full documentation string.
        /// </summary>
        /// <returns>The documentation header of the Lua function.</returns>
        public string GetFunctionHeader()
        {
            if (_documentationString.IndexOf("\n") == -1)
                return _documentationString;

            return _documentationString.Substring(0, _documentationString.IndexOf("\n"));
        }
        #endregion
    }
}
